package com.project.server.service.visitorRecord.impl;

import com.project.common.core.domain.BaseServer;
import com.project.server.domain.visitorRecord.TVisitorRecord;
import com.project.server.mapper.visitorRecord.TVisitorRecordMapper;
import com.project.server.service.visitorRecord.ITVisitorRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.project.common.utils.DateUtils.dateTime;

/**
 * 访客记录  业务层处理
 *
 * @author admin
 */
@Service
public class TVisitorRecordServiceImpl extends BaseServer implements ITVisitorRecordService {

    @Resource
    private TVisitorRecordMapper visitorRecordMapper;

    /**
     * 根据条件分页查询访客记录信息
     *
     * @param visitorRecord 访客记录信息
     * @return 访客记录信息集合信息
     */
    @Override
    public List<TVisitorRecord> selectVisitorRecordList(TVisitorRecord visitorRecord) {
        return visitorRecordMapper.selectVisitorRecordList(visitorRecord);
    }

    /**
     * 根据访客id查询访客纪录信息
     *
     * @param visitId 访客id
     * @return 访客记录信息
     */
    @Override
    public TVisitorRecord selectVisitorRecordByVisitId(Long visitId) {
        return visitorRecordMapper.selectVisitorRecordByVisitId(visitId);
    }

    /**
     * 根据访客电话查询访客记录信息
     *
     * @param visitPhone 访客电话
     * @return 访客记录信息
     */
    @Override
    public TVisitorRecord selectVisitorRecordByVisitPhone(String visitPhone) {
        return visitorRecordMapper.selectVisitorRecordByVisitPhone(visitPhone);
    }

    /**
     * 根据接待人id查询访客记录
     *
     * @param userId 接待人id
     * @return 访客记录信息
     */
    @Override
    public TVisitorRecord selectVisitorRecordByUserId(Long userId) {
        return visitorRecordMapper.selectVisitorRecordByUserId(userId);
    }

    /**
     * 根据审批人id查询访客记录信息
     *
     * @param approverId 审批人id
     * @return 访客记录信息
     */
    @Override
    public TVisitorRecord selectVisitorRecordByApproverId(Long approverId) {
        return visitorRecordMapper.selectVisitorRecordByApproverId(approverId);
    }

    /**
     * 查询待审批访客记录
     *
     * @param visitorRecord 访客信息
     * @return 待审批访客记录
     */
    @Override
    public List<TVisitorRecord> selectVisitorRecordByApproverState(TVisitorRecord visitorRecord) {
        return visitorRecordMapper.selectVisitorRecordByApproverState(visitorRecord);
    }

    /**
     * 根据openId查询该用户访问记录
     *
     * @param visitorRecord
     * @return 访客访问记录
     */
    @Override
    public List<TVisitorRecord> selectVisitorRecordByOpenId(TVisitorRecord visitorRecord) {
        return visitorRecordMapper.selectVisitorRecordByOpenId(visitorRecord);
    }

    /**
     * 增加访客记录
     *
     * @param visitorRecord 访客信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertVisitorRecord(TVisitorRecord visitorRecord) {
        if(visitorRecord.getVisitTime()==null) {
            String date = visitorRecord.getParams().get("startTime").toString();
            String date1 = visitorRecord.getParams().get("endTime").toString();
            date1 = date1.replace('/', '-');
            visitorRecord.setVisitTime(dateTime("yyyy-MM-dd HH:mm", date));
            visitorRecord.setLeftTime(dateTime("yyyy-MM-dd HH:mm", date1));
        }
        //获取前端传过来的身份证号码
        String idCard = visitorRecord.getVisitCard();
        //获取性别数字
        int sexNum = Integer.valueOf(idCard.substring(16, 17));
        if (sexNum % 2 == 0) {
            visitorRecord.setVisitSex("1");
        } else {
            visitorRecord.setVisitSex("0");
        }
        //获取当前年月日
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayNow = cal.get(Calendar.DATE);
        //获取身份证中的年月日
        int year = Integer.valueOf(idCard.substring(6, 10));
        int month = Integer.valueOf(idCard.substring(10, 12));
        int day = Integer.valueOf(idCard.substring(12, 14));
        //计算年龄
        if ((month < monthNow) || (month == monthNow && day <= dayNow)) {
            int age = yearNow - year;
            visitorRecord.setVisitAge(age);
        } else {
            int age = yearNow - year - 1;
            visitorRecord.setVisitAge(age);
        }
        return visitorRecordMapper.insertVisitorRecord(visitorRecord);
    }

    /**
     * 修改访客记录信息
     *
     * @param visitorRecord 访客信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateVisitorRecord(TVisitorRecord visitorRecord) {
        //获取前端传过来的身份证号码
        String idCard = visitorRecord.getVisitCard();
        //获取性别数字
        int sexNum = Integer.valueOf(idCard.substring(16, 17));
        if (sexNum % 2 == 0) {
            visitorRecord.setVisitSex("1");
        } else {
            visitorRecord.setVisitSex("0");
        }
        //获取当前年月日
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayNow = cal.get(Calendar.DATE);
        //获取身份证中的年月日
        int year = Integer.valueOf(idCard.substring(6, 10));
        int month = Integer.valueOf(idCard.substring(10, 12));
        int day = Integer.valueOf(idCard.substring(12, 14));
        //计算年龄
        if ((month < monthNow) || (month == monthNow && day <= dayNow)) {
            int age = yearNow - year;
            visitorRecord.setVisitAge(age);
        } else {
            int age = yearNow - year - 1;
            visitorRecord.setVisitAge(age);
        }
        return visitorRecordMapper.updateVisitorRecord(visitorRecord);
    }

    /**
     * 修改审批状态
     *
     * @param visitorRecord 访客信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateVisitorRecordApproverState(TVisitorRecord visitorRecord) {
        super.update(visitorRecord);
        return visitorRecordMapper.updateVisitorRecordApproverState(visitorRecord);
    }

    /**
     * 根据访客id删除访客记录信息
     *
     * @param visitId 访客id
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteVisitorRecordByVisitId(Long visitId) {
        return visitorRecordMapper.deleteVisitorRecordByVisitId(visitId);
    }

    /**
     * 批量删除访客记录信息
     *
     * @param visitIds 需要删除的访客id
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteVisitorRecordByVisitIds(Long[] visitIds) {
        return visitorRecordMapper.deleteVisitorRecordByVisitIds(visitIds);
    }

}
